import pandas as pd
import numpy as np
from scipy import stats

def process(df: pd.DataFrame, threshold: float = 3.0) -> pd.DataFrame:
    '''使用均值替换离群值
    
    Args:
        df: 输入数据框
        threshold: Z-score阈值，默认为3
    '''
    try:
        # 计算用电量的Z值
        z_scores = np.abs(stats.zscore(df.iloc[:, 1]))     
        # 均值替换离群值
        mean_value = df.iloc[:, 1].mean()   
        outlier_count = np.sum(z_scores > threshold)
        df.iloc[:, 1] = np.where(z_scores > threshold, mean_value, df.iloc[:, 1])
        print(f"mean: 替换了{outlier_count}个离群值")
        return df
    except Exception as e:
        print(f"mean替换处理错误: {str(e)}")
        return None 